package com.example.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.VO.UserVO;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class LoginController {
    @Autowired
    UserService userService;

    @PostMapping("/login")
    @ResponseBody
    public UserVO login(@RequestBody JSONObject jsonObject){
        String username = jsonObject.getString("username");
        String password = jsonObject.getString("password");
        UserVO result = new UserVO();
        result.setCode(0);

        if(userService.login(username,password)){
            result.setCode(1);
        }

        return result;
    }

    @PostMapping("/regist")
    @ResponseBody
    public UserVO regist(@RequestBody JSONObject jsonObject){
        String username = jsonObject.getString("username");
        String password = jsonObject.getString("password");
        UserVO result = new UserVO();
        result.setCode(0);

        if(userService.regist(username,password)){
            result.setCode(1);
        }

        return result;
    }


}
